package com.globalblue;

import java.io.File;

import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

import com.globalblue.view.ImprovedMainWindow;

public class ProgressBarDisplay extends SwingWorker<Boolean, Integer> {
	long fileSize = 0;
	String destinationFile;
	ImprovedMainWindow window;

	public ProgressBarDisplay(long fileSize, ImprovedMainWindow window) {
		this.fileSize = fileSize;
		this.destinationFile = Configurations.getZipFileLocation();
		this.window = window;
	}

	
	@Override
	protected Boolean doInBackground() throws Exception {
		JPanel newPanel = new JPanel();
		JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(272, 65, 339, 130);
		newPanel.add(progressBar);
		window.changeMainWindowContent(newPanel);
		long downloadedSize = 0;
		int percentage = 0;

		File file = new File(Configurations.getZipFileLocation());
		do {
			downloadedSize = file.length();
			percentage = (int) (downloadedSize * 100 / fileSize);
			progressBar.setValue(percentage);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while (fileSize > downloadedSize);

		return true;
	}
}
